<?php

namespace app\api\controller;
use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Factory;


use app\model\OrderModel;
use support\Log;
use support\Request;
use think\facade\Db;
use WeChatPay\Builder;
use WeChatPay\Crypto\AesGcm;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Formatter;
use WeChatPay\Util\PemUtil;

class NotifyController extends Base
{
    public function wx(Request $request){
        $info = $request->header();
        Log::error('lizhili123456:'.'666666');
        $inWechatpaySignature = $info['wechatpay-signature'];
        $inWechatpayTimestamp = $info['wechatpay-timestamp'];
        $inWechatpaySerial = $info['wechatpay-serial'];
        $inWechatpayNonce = $info['wechatpay-nonce'];

        if (!$inWechatpaySignature or !$inWechatpayTimestamp or !$inWechatpaySerial or !$inWechatpayNonce) {
            e('非法请求');
        }
        $config = [
            'v3_secret'=>config('wx.pay_key')
        ];
        $inBody = $request->rawBody();// 请根据实际情况获取，例如: file_get_contents('php://input');

        Log::error('lizhili123456:'.json_encode($inBody));
        $apiv3Key = $config['v3_secret'];// 在商户平台上设置的APIv3密钥
// 根据通知的平台证书序列号，查询本地平台证书文件，这里是自己生成的证书
        $platformPublicKeyInstance = Rsa::from(file_get_contents(base_path().'/zhengshu/wx/wechatpay.pem'), Rsa::KEY_TYPE_PUBLIC);
// 检查通知时间偏移量，允许5分钟之内的偏移
        $timeOffsetStatus = 300 >= abs(Formatter::timestamp() - (int)$inWechatpayTimestamp);
        $verifiedStatus = Rsa::verify(
        // 构造验签名串
            Formatter::joinedByLineFeed($inWechatpayTimestamp, $inWechatpayNonce, $inBody),
            $inWechatpaySignature,
            $platformPublicKeyInstance
        );
        if ($timeOffsetStatus && $verifiedStatus) {
            // 转换通知的JSON文本消息为PHP Array数组
            $inBodyArray = (array)json_decode($inBody, true);
            // 使用PHP7的数据解构语法，从Array中解构并赋值变量
            ['resource' => [
                'ciphertext'      => $ciphertext,
                'nonce'           => $nonce,
                'associated_data' => $aad
            ]] = $inBodyArray;
            // 加密文本消息解密
            $inBodyResource = AesGcm::decrypt($ciphertext, $apiv3Key, $nonce, $aad);
            // 把解密后的文本转换为PHP Array数组
            $inBodyResourceArray = (array)json_decode($inBodyResource, true);
            // print_r($inBodyResourceArray);// 打印解密后的结果
            $result=$inBodyResourceArray;
            Log::error('lizhili1236:'.json_encode($result));
            if ($result['trade_state'] == 'SUCCESS') {
                $out_trade_no = $result['out_trade_no'];
                $pay_money= $result['amount']['payer_total']/100;

                $share=OrderModel::where('out_trade_no','zong_'.$out_trade_no)->where('pay_status',0)->find();
                if($share){
                    $arr=[
                        'pay_status'=>1,
                        'order_time'=>$result['success_time'],
                        'pay_price'=>$pay_money,
                        'notify_json'=>$inBodyResource,
                    ];
                    OrderModel::where('id',$share['id'])->update($arr);
                    thr('SUCCESS','','成功');
                }

            }
        }
    }

}